import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.0
import AIClient.module 1.0

Page {

    signal createApplication(string app_name, string trainjob_id, string description)
    //signal getTrainjob(string uid)
    signal cancel()
    signal error(string msg)
    signal viewTrainjob(string trainjob_id)

//    AIClient {
//        id: client
//    }

//    TipMessage {
//        id: tip
//        width: parent.width
//    }

//    function setToken(token) {
//        m_token = token
//    }

//    property string m_token: ""
    property int m_mode: 1
    function setMode(mode) {
        m_mode = mode
    }

    property string m_uid: ""

    function setConfig(name, description, trainjob_index)
    {
        app_name.text = name
        app_description.text = description
        trainjobCombo.currentIndex = trainjob_index
    }

    function updateMode() {
        if ( m_mode === 1 )
        {
            app_name.text = ""
            app_description.text = ""
            trainjobCombo.currentIndex = -1

            app_name.enabled = true
            app_description.enabled = true
            trainjobCombo.enabled = true
            m_uid = ""
            btn.text = "创建"
            btn.enabled = true
        }
        else
        {
            app_name.enabled = false
            app_description.enabled = false
            trainjobCombo.enabled = false
            btn.text = "关闭"
            btn.enabled = false
        }
    }


    id: app_create
    title: "创建AI应用"
    visible: false
    //visibility: "Maximized"
//    modality: Qt.ApplicationModal
//    minimumWidth: Screen.desktopAvailableWidth*0.3
//    maximumWidth: Screen.desktopAvailableWidth
//    minimumHeight: Screen.desktopAvailableHeight*0.75
//    maximumHeight: Screen.desktopAvailableHeight
    width: Screen.desktopAvailableWidth*0.5
    height: Screen.desktopAvailableHeight*0.75
//    color: "azure"

//    Component.onCompleted: {
//        updateModel()
//        updateDataset()
//    }

    function updateTrainjob() {
        removeAllTrainjobItem()
        client.setConfig(m_token)
        var res = client.list_trainjob()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        //tip.showmsg("获取数据集成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            data[i]["text"] = data[i]["name"]
            data[i]["color"] = "black"
            addTrainjobItem(jres["data"][i])
        }
    }

    function addTrainjobItem(item) {
        trainjobModel.append(item)
    }

    function removeAllTrainjobItem() {
        trainjobModel.clear()
    }

    function removeTrainjobItem(item) {
        trainjobModel.remove(item)
    }

    ListModel {
        /* 数据项 */
        id: trainjobModel
        ListElement {
            uid: "uuid";
            name: "red";
            create_date: "2020";
            text: "t";
            color: "black";
        }
    }

    Rectangle{
        anchors.fill: parent
        color:"azure"
        width: app_create.width
        height: app_create.height


        GridLayout {

            id: grid_app_create
            rows: 5
            columns: 3
            anchors.topMargin: 50
            anchors.bottomMargin: 50
            anchors.leftMargin: 50
            anchors.rightMargin: 50
            anchors.fill: parent
            columnSpacing: 5
            rowSpacing: 15
            Text {
                text: qsTr("名称")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.preferredWidth: 180
                color: "black"
                Layout.column: 0
                Layout.row: 0
            }
            TextField {
                id: app_name
                text: ""
                selectByMouse: true
                font.pointSize: 12
                Layout.minimumHeight: 12*2
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                Layout.fillWidth: true
                Layout.row: 0
                Layout.column: 1
            }

            Text {
                text: qsTr("训练任务")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                color: "black"
                Layout.column: 0
                Layout.row: 1
            }
            ComboBox {
                id: trainjobCombo
                currentIndex: -1
                Layout.column: 1
                Layout.row: 1
                font.pointSize: 12
                Layout.minimumHeight: 12*3
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                Layout.fillWidth: true
                model: trainjobModel
                textRole: "text"
            }
            OrangeButton {
                text: "查看训练任务"
                Layout.minimumHeight: 25
                Layout.maximumHeight: 60
                Layout.minimumWidth: 180
        //Layout.preferredWidth: 500
                Layout.row: 1
                Layout.column: 2

                enabled: trainjobCombo.currentIndex>=0
                onClicked: {
                    var trainjob_index = trainjobCombo.currentIndex
                    viewTrainjob(trainjobModel.get(trainjob_index).uid)
                }
            }

            Text {
                text: qsTr("描述")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.maximumHeight: 600
                //Layout.fillWidth: true
                Layout.row: 2
                Layout.column: 0
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 400
                Layout.minimumHeight: 12*5
                Layout.maximumHeight: 600
                //height: 500
                //Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.row: 2
                Layout.column: 1
                id: app_description
            }
//            ScrollView{
//                Layout.maximumHeight: 600
//                Layout.minimumHeight: 12*5
//                Layout.preferredHeight: 400
//                Layout.fillWidth: true
//                Layout.row: 2
//                Layout.column: 1
//                TextArea {
//                    id: app_description
//                    text: ""
//                    selectByMouse: true
//                    font.pointSize: 12

//                    background: Rectangle{
//                        anchors.fill: parent
//                        width: 500
//                        color: "white"
//                        border.color: "gray"
//                    }
//                }
//            }

//            Rectangle{
//                Layout.preferredHeight: 40
//                Layout.fillHeight: true
//                Layout.row: 3
//                Layout.column: 0
//            }
            RowLayout {
                Layout.row: 3
                Layout.column: 1
                OrangeButton {
                    id: btn
                    text: "确定"
                    Layout.minimumHeight: 25
                    Layout.maximumHeight: 60
                    Layout.minimumWidth: 180
            //Layout.preferredWidth: 500


                    onClicked: {

                    if ( m_mode === 1 )
                    {
                        var trainjob_index = trainjobCombo.currentIndex
                        if ( trainjob_index < 0 )
                        {
                            error("请选择训练任务！")
                            return
                        }

                        if ( app_name.text.length <= 0 )
                        {
                            error("请输入AI应用名称！")
                            return
                        }
                        if ( app_description.text.length <= 0 )
                        {
                            error("请输入AI应用描述！")
                            return
                        }
                        createApplication(app_name.text, trainjobModel.get(trainjob_index).uid, app_description.text)
                        return

                    }

                }
            }
            OrangeButton {
                text: "返回"
//            Layout.columnSpan: 2
                Layout.maximumHeight: 60
                Layout.minimumWidth: 180
            //Layout.preferredWidth: 500
                Layout.minimumHeight: 12*1.5

                onClicked: {
                    cancel()
                }
            }
            Rectangle {

    //                Layout.minimumHeight: 200
                Layout.fillHeight: true
                Layout.fillWidth: false
                Layout.maximumHeight: 60
                color: "transparent"
                Layout.columnSpan: 0
            }
        }
        Rectangle {
            Layout.row: 4
            Layout.column: 0
//                Layout.minimumHeight: 200
            Layout.fillHeight: true
            Layout.fillWidth: true
            color: "transparent"
            Layout.columnSpan: 3
        }

//        TextEdit {
//            id: content
//            text: m_content
//            width: 350
//            Layout.preferredHeight: 500
//        }
       }
    }
    function isRealNum(val){
        // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，

      if(val === "" || val === null){
            return false;
      }
       if(!isNaN(val)){
      //对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
       //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
        return true;
      }

     else{
        return false;
      }
    }
    signal success()
}
